package com.dongchedi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongchedi.entity.CustomerAddr;
import com.dongchedi.entity.CustomerLogin;
import com.dongchedi.entity.ProductInfo;
import com.dongchedi.entity.ProductPicInfo;
import com.dongchedi.mapper.CustomerAddrMapper;
import com.dongchedi.mapper.ProductInfoMapper;
import com.dongchedi.mapper.ProductPicInfoMapper;
import com.dongchedi.service.ProductInfoService;
import com.dongchedi.utils.HostHolder;
import com.dongchedi.utils.constant.ColorConstant;
import com.dongchedi.utils.constant.RedisConstantKey;
import com.dongchedi.utils.page.PageRequest;
import com.dongchedi.utils.page.PageResult;
import com.dongchedi.utils.page.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author leibidong
 * @description   商品详情实现类
 * @date 2021/6/26
 */

@Service
public class ProductInfoServiceImpl implements ProductInfoService {
    @Autowired
    ProductInfoMapper productInfoMapper;
    @Autowired
    ProductPicInfoMapper productPicInfoMapper;
    @Autowired
    CustomerAddrMapper customerAddrMapper;
    @Resource
    RedisTemplate<String,Object> redisTemplate;
    @Resource
    HostHolder hostHolder;
    /*
     * @Author: leibidong
     * @Description: 查询出显示热销的前三商品
     * @Date: 2021/6/26 20:01
     * @Param: [pageNum, pageSize]
     * @Return: com.alibaba.fastjson.JSONArray
     **/
    @Override
    public JSONArray hotSale(Integer pageNum, Integer pageSize) {
        LocalDate today = LocalDate.now();
        String key= RedisConstantKey.getHotsaleKey(today);
        if(redisTemplate.opsForValue().get(key)==null){
            PageHelper.startPage(pageNum, pageSize);
            List<ProductInfo> list = productInfoMapper.hotQuery();
            PageRequest pageRequest = new PageRequest(pageNum, pageSize);
            PageResult pageResult = PageUtil.getPageResult(pageRequest, new PageInfo<ProductInfo>(list));
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < pageResult.getContent().size(); i++) {
                ProductInfo productInfo = (ProductInfo) pageResult.getContent().get(i);
                Integer id = productInfo.getId();
                List<ProductPicInfo> list1 = productPicInfoMapper.queryById(id);
                JSONObject node = new JSONObject();
                node.put("id", id);
                node.put("price", productInfo.getPrice());
                node.put("averageCost", productInfo.getAverageCost());
                node.put("picDesc", list1.get(0).getPicDesc());
                node.put("picUrl", list1.get(0).getPicUrl());
                jsonArray.add(node);

            }
            redisTemplate.opsForValue().set(key,jsonArray.toJSONString(),86400, TimeUnit.SECONDS);
            return jsonArray;
        }
        else {
            String temp=(String) redisTemplate.opsForValue().get(key);
            JSONArray jsonArray=JSONArray.parseArray(temp);
            System.out.println(jsonArray);
            return  jsonArray;
        }
    }
    /*
     * @Author: leibidong
     * @Description: 根据商品id查询商品具体信息
     * @Date: 2021/6/26 20:01
     * @Param: [id, customerId]
     * @Return: com.alibaba.fastjson.JSONObject
     **/
    @Override
    public JSONObject productInfo(Integer id, Integer customerId) {
        CustomerLogin customerLogin=hostHolder.getUser();
        if(customerId.equals(customerLogin.getId())) {
            Integer integer = productInfoMapper.searchProductId(id, customerId);
            if (integer != null) {
                String key = RedisConstantKey.getProductKey(customerId, integer);
                if (redisTemplate.opsForValue().get(key) != null) {
                    String temp = (String) redisTemplate.opsForValue().get(key);
                    return JSONObject.parseObject(temp);
                } else {
                    ProductInfo productInfo = productInfoMapper.queryById(integer, customerId);
                    List<ProductPicInfo> list = productPicInfoMapper.queryById(id);
                    List<ProductPicInfo> listSlave = productPicInfoMapper.queryByIdSlave(id);
                    JSONArray jsonArray = new JSONArray();
                    for (int i = 0; i < list.size(); i++) {
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("picDesc", list.get(i).getPicDesc());
                        jsonObject1.put("picUrl", list.get(i).getPicUrl());
                        jsonArray.add(jsonObject1);
                    }
                    JSONArray jsonArray1 = new JSONArray();
                    for (int i = 0; i < listSlave.size(); i++) {
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("picDesc", listSlave.get(i).getPicDesc());
                        jsonObject1.put("picUrl", listSlave.get(i).getPicUrl());
                        jsonArray1.add(jsonObject1);
                    }
                    CustomerAddr customerAddr = customerAddrMapper.queryById(customerId);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("id", integer);
                    jsonObject.put("customerId", productInfo.getCustomerId());
                    jsonObject.put("name", productInfo.getName());
                    jsonObject.put("price", productInfo.getPrice());
                    jsonObject.put("averageCost", productInfo.getAverageCost());
                    jsonObject.put("collectStatus", productInfo.getCollectStatus());
                    jsonObject.put("colorType", ColorConstant.HEIHUI.getNameByIndex(productInfo.getColorType()));
                    jsonObject.put("recommendDescript", productInfo.getRecommendDescript());
                    jsonObject.put("descript", productInfo.getDescript());
                    jsonObject.put("address", customerAddr.getAddress());
                    jsonObject.put("MasterPicture", jsonArray);
                    jsonObject.put("SlavePicture", jsonArray1);
                    redisTemplate.opsForValue().set(key, jsonObject.toJSONString(), 3600, TimeUnit.SECONDS);
                    return jsonObject;
                }
            } else {
                ProductInfo productInfo = productInfoMapper.queryById(id, 0);
                productInfo.setCustomerId(customerId);
                productInfoMapper.initProductInfo(productInfo);
                List<ProductPicInfo> list = productPicInfoMapper.queryById(id);
                List<ProductPicInfo> listSlave = productPicInfoMapper.queryByIdSlave(id);
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < list.size(); i++) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("picDesc", list.get(i).getPicDesc());
                    jsonObject1.put("picUrl", list.get(i).getPicUrl());
                    jsonArray.add(jsonObject1);
                }
                JSONArray jsonArray1 = new JSONArray();
                for (int i = 0; i < listSlave.size(); i++) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("picDesc", listSlave.get(i).getPicDesc());
                    jsonObject1.put("picUrl", listSlave.get(i).getPicUrl());
                    jsonArray1.add(jsonObject1);
                }
                CustomerAddr customerAddr = customerAddrMapper.queryById(customerId);
                JSONObject jsonObject = new JSONObject();

                jsonObject.put("id", productInfo.getId());
                jsonObject.put("customerId", productInfo.getCustomerId());
                jsonObject.put("name", productInfo.getName());
                jsonObject.put("price", productInfo.getPrice());
                jsonObject.put("averageCost", productInfo.getAverageCost());
                jsonObject.put("collectStatus", productInfo.getCollectStatus());
                jsonObject.put("colorType", ColorConstant.HEIHUI.getNameByIndex(productInfo.getColorType()));
                jsonObject.put("recommendDescript", productInfo.getRecommendDescript());
                jsonObject.put("descript", productInfo.getDescript());
                jsonObject.put("address", customerAddr.getAddress());
                jsonObject.put("MasterPicture", jsonArray);
                jsonObject.put("SlavePicture", jsonArray1);
                String key = RedisConstantKey.getProductKey(customerId, productInfo.getId());
                redisTemplate.opsForValue().set(key, jsonObject.toJSONString(), 3600, TimeUnit.SECONDS);
                return jsonObject;
            }
        }else {
            return  new JSONObject();
        }
    }
    /*
     * @Author: leibidong
     * @Description: 修改用户的收藏状态，如果collectStatus=1，就取消收藏，如果collectStatus=0，就添加收藏
     * @Date: 2021/6/26 20:02
     * @Param: [customerId, id, collectStatus]
     * @Return: com.alibaba.fastjson.JSONObject
     **/
    @Override
    public JSONObject collectStatus(Integer customerId, Integer id,Integer collectStatus) {
        JSONObject jsonObject=new JSONObject();
        CustomerLogin customerLogin=hostHolder.getUser();
        if(customerId.equals(customerLogin.getId())) {
            if (collectStatus == 1 ) {
                Integer integer = productInfoMapper.updateProductStatusToZero(customerId, id, collectStatus);
                if (integer != 0) {
                    String key = RedisConstantKey.getProductKey(customerId, id);
                    redisTemplate.delete(key);
                    jsonObject.put("collectStatus", 0);
                    return jsonObject;
                }
            } else if (collectStatus == 0 ) {
                Integer integer = productInfoMapper.updateProductStatusToOne(customerId,id, collectStatus);
                if (integer != 0) {
                    String key = RedisConstantKey.getProductKey(customerId, id);
                    redisTemplate.delete(key);
                    jsonObject.put("collectStatus", 1);
                    return jsonObject;
                }
            }
            return jsonObject;
        }else {
            return jsonObject;
        }
    }
}
